<template>
  <!-- 单选题 类名single 已查看解析is-answer  -->
  <div class="problem-box single is-answer p12">
    <div class="peoblem-main">
      <div class="problem-body">
        <div class="porblem-title">
          <i class="tag-type">单选</i>
          <h1 class="richText" v-html="title"></h1>
        </div>
        <div class="problem-content">
          <!-- 做题状态 li添加class 选中：checked, 正确：right,错误：error -->
          <ul class="problem-options">
            <li
              v-for="(value, key, index) in option"
              :key="index"
              @click="check_options(key)"
              :class="(show_answer?(is_test?((value.is_checked?(value.is_checked==value.is_correct?'right answer ':'error'):(value.is_correct?'answer':''))):value.is_correct?'answer':''):(is_test?(value.is_checked?'checked':''):(value.is_checked?(value.is_checked==value.is_correct?'right answer ':'error'):(done_show_answer?(value.is_correct?'answer':''):''))))"
            >
             
              <i class="check-box"></i>
              <i class="answer-box"></i>
              <div class="option">
                <span>{{key}}、</span>
                <p class="richText" v-html="value.option"></p>
              </div>
            </li>
          </ul>
        </div>
        <div class="problem-answer p12" v-if="show_answer || done_show_answer">
          <div class="anser">
            <span>答案：</span>
            <p class="richText" v-html="correct_option"></p>
          </div>
          <div class="analysis">
            <span>解析：</span>
            <p class="richText" v-html="parse"></p>
          </div>
        </div>
        <div class="problem-note bgc-fff bor4 p12" v-if="notes != '' && show_note">
          <div class="note-title">
            <h5>笔记</h5>
          </div>
          <div class="note-content">
            <p class="richText" v-html="notes"></p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { QuestionSubmit } from "../../api/api";
export default {
  props: {
    // 题目详情
    details: {
      type: Object,
      default: () => {}
    },
    //笔记显示
    show_note: {
      type: Boolean,
      default: false
    },
    // 答案显示
    show_answer: {
      type: Boolean,
      default: false
    },
    //判断是否为考试
    is_test: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      title: this.details.title, //标题
      option: this.details.option, //选项
      correct_option: this.details.correct_option, //答案
      parse: this.details.parse, //解析
      notes: this.details.notes, //笔记
      done_show_answer: this.details.done_show_answer //是否提交答案
    };
  },
  methods: {
    // 选题
    check_options(option) {
      if (!this.done_show_answer) {
        if (!this.show_answer) {
          Object.keys(this.option).forEach(k => {
            if (k == option) {
              this.option[k].is_checked = true;
            } else {
              this.option[k].is_checked = false;
            }
          });
          this.submit_answer(option);
        }
      }
    },
    // 提交答案
    submit_answer(answer) {
      let data = {
        answer: [answer],
        currentId: this.details.id
      };
      QuestionSubmit(data)
        .then(res => {
          console.log(res);
          if (res.code == 1) {
            this.$emit("submit-answer", { id: this.details.id });
            if (!this.is_test) {
              this.done_show_answer = true;
            }
          } else {
            this.$toast("答案提交失败");
          }
        })
        .catch(err => {
          this.$toast("答案提交失败");
        });
    }
  }
};
</script>
<style scoped lang="scss">
@import "../../assets/css/problem.css";
</style>


